import urllib.request
import time
from lxml import etree

for i in range(1, 344):
    if i == 1:
        url = 'https://s.fuxs2.cc/gudai/21933.html'
    else:
        url = f'https://s.fuxs2.cc/gudai/21933_{i}.html'
    headers = {
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
        # 'accept-encoding': 'gzip, deflate, br, zstd',
        'accept-language': 'zh-CN,zh;q=0.9',
        'cache-control': 'no-cache',
        'cookie': 'cf_clearance=0oQOznNyZvvlKo2we8ocTCYOS.J3BvKv9AlS.jMNFd4-1724154108-1.2.1.1-3ep.e5Q8Zh.OUAwShbPMYKdwzbp_dw84DBm7LQykg3B34uitM8q29aT9vhPoYvc1FmHgQn6CP1SeXj2HqKY6IZUGXxMJLpjqNHKy6HlaTdtTU7zJBOvD4socbekcmbxWRDoUfnJ32I0Pmfnw1taMRZr3I8SZbjJBxX8iyO5nU77TVKnXBkm7r4fySAnSw_we08Vo1wGAQH9KqLAFnShd8qUYXeKOrrfqvbzfwHiiOBXQ73rj1INxo42449EdIojKpZSaYNlvVjW_Msl_sE32JxkS5OVCF838XTnKRHUB1V1uM8HY9l6jNmuUr7JHJUOFBnNkld_33tmWXeDdITVlCgjmGJhelJRAKJuH_cbE.XMTH1cw5E_hF7_O0l7dmOkK',
        'pragma': 'no-cache',
        'priority': 'u=0, i',
        'referer': 'https://s.fuxs2.cc/gudai/21933.html',
        'sec-ch-ua': '"Not)A;Brand";v="99", "Google Chrome";v="127", "Chromium";v="127"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
        'sec-fetch-dest': 'document',
        'sec-fetch-mode': 'navigate',
        'sec-fetch-site': 'same-origin',
        'sec-fetch-user': '?1',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36',
    }

    request = urllib.request.Request(url=url, headers=headers)

    response = urllib.request.urlopen(request)

    content = response.read().decode('utf-8')

    tree = etree.HTML(content)

    text = tree.xpath('//div[@class="wznrb"]/text()')

    print('正在爬取第', i, '章')
    time.sleep(1)

    for j in text:
        # print(i)

        with open(f'./mcww/满朝文武都能听到我的心声.text', 'a', encoding='utf-8') as fp:
            fp.write(str(j)+'\n')

